package com.example.comm.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.comm.Bean.User;
import com.example.comm.R;
import com.example.comm.activity.Login;
import com.example.comm.activity.MyInfo;
import com.example.comm.activity.MyPush;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class FragmentMine extends Fragment {
    private TextView username;
    //private TextView nickname;
    private Button loginout;

    private LinearLayout myinfo;
    private LinearLayout mypush;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentmine,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        //加载个人信息
        getMyinfo();
        loginout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobUser.logOut();
                startActivity(new Intent(getActivity(), Login.class));
                getActivity().finish();

            }
        });

        myinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到我的信息界面
                startActivity(new Intent(getActivity(), MyInfo.class));
            }
        });

        mypush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyPush.class));
            }
        });
    }

    private void getMyinfo() {
        BmobUser bu=BmobUser.getCurrentUser(BmobUser.class);
        String Id=bu.getObjectId();
        BmobQuery<User> bmobQuery=new BmobQuery<User>();
        bmobQuery.getObject(Id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if(e==null){
                    username.setText(user.getUsername());
                    //nickname.setText(user.getNickname());
                }else{
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView(View view) {
        username=view.findViewById(R.id.username);
        //nickname=view.findViewById(R.id.nickname);
        loginout=view.findViewById(R.id.loginout);
        myinfo=view.findViewById(R.id.myinfo);
        mypush=view.findViewById(R.id.mypush);

    }
}
